<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入axios 方便操作 -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .login,
        .register {
            width: 500px;
            padding: 10px;
            padding-bottom: 30px;
            margin: 100px auto;
            background: #ddd;
            text-align: center;
        }

        .login div,
        .register div {
            display: flex;
            width: 100%;
            height: 30px;
            background-color: #fff;
            margin: 20px 0;
            font-size: 14px;
            align-items: center;
            padding-left: 5px;

        }

        .login input,
        .register input {
            flex: 1;
            border: 0;
            padding: 0 5px;
            outline: none;
        }

        .login button,
        .register button {
            width: 100%;
            height: 30px;
            background: red;
            color: #fff;
            border: 0;
            cursor: pointer;
        }

        .login h3,
        .register h3 {
            position: relative;
        }

        .login h3 a,
        .register h3 a {
            font-size: 12px;
            position: absolute;
            right: 0;
            color: red;
        }

        .register {
            display: none;
        }
    </style>
</head>

<body>
    <!-- 登录 -->
    <div class="login">
        <h3>
            login 页面
            <a href="javascript:;" id="register">注册一下</a>
        </h3>
        <div>
            用户名：
            <input type="text" id="username" autocomplete="off" placeholder="请输入用户名">
        </div>
        <div>
            密&emsp;码：
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <button id="submit">登录</button>
    </div>
    <!-- 注册 -->
    <div class="register">
        <h3>
            注册页面
            <a href="javascript:;" id="login">已有账号,回去登录</a>
        </h3>
        <div>
            用户名：
            <input type="text" id="reg_username" autocomplete="off" placeholder="请输入用户名">
        </div>
        <div>
            密&emsp;码：
            <input type="password" id="reg_password" placeholder="请输入密码">
        </div>
        <div>
            头&emsp;像：
            <input type="file" id="file" placeholder="请输入密码">
        </div>
        <button id="reg_submit">确定</button>
    </div>

</body>
<script>
    //--------------------- 页面的切换
    const registerPage = document.querySelector(".register")
    const loginPage = document.querySelector(".login")
    register.onclick = () => {
        loginPage.style.display = "none"
        registerPage.style.display = "block"
    }
    login.onclick = () => {
        loginPage.style.display = "block"
        registerPage.style.display = "none"
    }
    //-----------------------注册页
    reg_submit.onclick = () => {
        if (!(reg_username.value) && !reg_password.value)
            return alert("不能为空");
        const fd = new FormData()
        fd.append("avatar", file.files[0]);
        fd.append("password", reg_password.value)
        fd.append("username", reg_username.value)
        // 发送头像和用户名密码去注册
        axios.post("/login", fd).then(res => {
            alert("注册" + (res.data.ok ? "成功" : "失败"))
            location.reload();
        })
    }
    // ----------------------登录页
    submit.onclick = () => {
        if (!(username.value) && !password.value)
            return alert("不能为空");
        axios.post("/login/find", {
            username: username.value,
            password: password.value
        })
            .then(res => {
                console.log(res.data)
                if (res.data.ok) {
                    localStorage.setItem("username", username.value)
                    localStorage.setItem("avatar", res.data.avatar)
                    location.href = '/'
                } else {
                    alert("密码错误")
                }
            })
    }
</script>

</html>